//: src/com/silvtar/rsync/SSRsyncd.java
/* 
 * Created by 朴晟宁 <piaosn@gmail.com> on 5/25/2013. 
 * Copyright (c) 2013 朴晟宁. All rights reserved.
 * 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program;
 * 
 * *********************************************************************
 * 
 * Modify History:
 * Date        Version    Description
 * 5/26/2013   0.1        Create the class. 
 */

package com.silvtar.rsync;

import java.util.LinkedList;

import com.silvtar.sync.model.SSAnalyzer;
import com.silvtar.sync.model.SSTreeNode;

/**
 * rsync守护进程打包类 本类会根据需要同步的文件夹自动生成rsyncd配置文件。并同步。 本类为分析器特制
 * 
 * @author 朴晟宁/Silvtar
 * @author piaosn@gmail.com
 * @version 0.1
 */
public class SSRsyncDaemonForAnalyzer extends SSRsyncDaemon {

    // 以下变量为分析器使用，其他时候无效
    String                 rootPath;
    LinkedList<SSTreeNode> inQueue;
    LinkedList<SSTreeNode> outQueue;

    /**
     * 配置rsync daemon，准备接下来的同步。
     */
    public void configRsyncDaemon_Analyzer(SSAnalyzer analyzer, String rootPath,
            String authUsers, String uid, String gid, String rsyncRootPath,
            int port) {

        configRsyncDaemon(authUsers, uid, gid, rsyncRootPath, port);

        this.rootPath = rootPath;
        this.outQueue = analyzer.getInQueue();
        this.inQueue = analyzer.getOutQueue();

        // 传出队列
        while (!outQueue.isEmpty()) {
            SSTreeNode currentTreeNode = outQueue.pop();
            writeItem(currentTreeNode);
        }

        // 传入队列
        while (!inQueue.isEmpty()) {
            SSTreeNode currentTreeNode = inQueue.pop();
            writeItem(currentTreeNode);
        }

        closeCST();
    }

    /**
     * 向配置文件中写入一个项目
     * 
     * @param currentTreeNode
     */
    public void writeItem(SSTreeNode currentTreeNode) {

        String path = rootPath + currentTreeNode.getRelativePath();
        super.writeItem(path);
    }

}
